package com.longrise.android.byjk.plugins.tabsecond;

import com.longrise.LEAP.Base.Objects.EntityBean;
import com.longrise.android.byjk.common.UrlConstants;
import com.longrise.android.byjk.model.AllCourseParentBean;
import com.longrise.android.loaddata.newloaddata.LoadDataManagerFather;
import com.longrise.common.datasource.remote.LoadDataManager;
import com.longrise.common.utils.DZZWTools;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by callxkj on 2017/10/23.
 * 第二个tab 保险医学
 */

public class SecondPresenter extends SecondContract.Presenter {

    private List<AllCourseParentBean> mParentBeanList = new ArrayList<>();

    @Override
    protected void init() {
       getSortFromNet();
    }

    /**
     * 获取课程分类列表
     */
    public void getSortFromNet() {
        EntityBean bean = new EntityBean();
        bean.set("areaid", "0");  //以前保宝的全国课程
        EntityBean fbean = new EntityBean();
        fbean.set("bean", bean);
        LoadDataManager.getInstance().callService2("", UrlConstants.BaseUrl,
                "bbt_course_sAllTrainPlanAssortmentsBean", fbean, new LoadDataManager.OnRequestCallListener2() {
                    @Override
                    public void onSuccess(String key, String service, Object result) {
                        parseSortData(result);
                    }

                    @Override
                    public void onError(String key,String service, LoadDataManagerFather.ResultType resultType) {

                    }

                    @Override
                    public void onFinished(String key, String service) {
                        DZZWTools.dismissDialog(null);
                    }
                });
    }


    /**
     * 解析课程分类
     *
     * @param result
     */
    public void parseSortData(Object result) {
        try {
            if (result != null) {
                EntityBean bean = (EntityBean) result;
                Integer resultstate = bean.getInt("resultstate");
                if (resultstate == 1) {
                    EntityBean resultBean = bean.getBean("result");
                    boolean isshowtips = resultBean.getBoolean("isshowtips");
                    if (isshowtips) {  //是否可以购买 不支持购买的显示提示
                        String tips = resultBean.getString("tips");
                    } else {
                        EntityBean[] results = resultBean.getBeans("assortment");
                        if (results != null) {
                            for (EntityBean resultbean : results) {
                                String assortmentname = resultbean.getString("assortmentname");
                                String assortmenttype = resultbean.getString("assortmenttype");
                                AllCourseParentBean courseBean = new AllCourseParentBean();
                                courseBean.setAssortmentname(assortmentname);
                                courseBean.setAssortmenttype(assortmenttype);
                                mParentBeanList.add(courseBean);
                            }
                            mView.initAdapter(mParentBeanList);
                        } else {

                        }

                    }
                }
            }

        } catch (Exception e) {
            e.printStackTrace();
        }
    }



}
